<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-29
 * Time: 15:54
 */

namespace app\api\controller\v1;



use app\api\model\GoodsOrder;
use app\api\model\LinUser;
use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\model\UserMoney;
use LinCmsTp5\exception\ParameterException;
use think\Db;
use think\Exception;

class Order
{
    /**
     * 商城订单
     * @validate('PagingParameter')
     */
    public function getGoodsList($page,$limit,$user_no,$name,$mobile,$address,$status,$start_date,$end_date)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($mobile) {
            $where[] = ['mobile','like','%'.$mobile.'%'];
        }
        if($address) {
            $where[] = ['address','like','%'.$address.'%'];
        }
        if($status) {
            $where[] = ['goods_order.status', 'eq' ,$status];
        } else {
            $where[] = ['goods_order.status', 'gt' ,1];
        }
        if($start_date) {
            $where[] = ['pay_time', 'gt', strtotime($start_date)];
        }
        if($end_date) {
            $where[] = ['pay_time', 'lt', strtotime($end_date)];
        }
        $query = GoodsOrder::hasWhere('user',function ($query1) use($user_no){
           if($user_no) {
               $query1->where('user_no','like','%'.$user_no.'%');
           }
        })->with('content.img,user')->where($where)->order('pay_time desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 2) {
                $item['status_name'] = '待发货';
            } elseif($item['status'] == 3){
                $item['status_name'] = '待收货';
            } elseif($item['status'] == 4){
                $item['status_name'] = '已完成';
            } elseif($item['status'] == 5){
                $item['status_name'] = '已申请退款';
            } elseif($item['status'] == 6){
                $item['status_name'] = '退款完成';
            }
            if($item['payment'] == 1) {
                $item['payment_name'] = '微信支付';
            } elseif($item['payment'] == 2) {
                $item['payment_name'] = '余额支付';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    // 导出信息
    public function getGoodsExcelList($user_no,$name,$mobile,$address,$status,$start_date,$end_date)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($mobile) {
            $where[] = ['mobile','like','%'.$mobile.'%'];
        }
        if($address) {
            $where[] = ['address','like','%'.$address.'%'];
        }
        if($status) {
            $where[] = ['goods_order.status', 'eq' ,$status];
        } else {
            $where[] = ['goods_order.status', 'gt' ,1];
        }
        if($start_date) {
            $where[] = ['pay_time', 'gt', strtotime($start_date)];
        }
        if($end_date) {
            $where[] = ['pay_time', 'lt', strtotime($end_date)];
        }
        return GoodsOrder::hasWhere('user',function ($query1) use($user_no){
            if($user_no) {
                $query1->where('user_no','like','%'.$user_no.'%');
            }
        })->with('content,user')->where($where)->order('pay_time desc')->select()->each(function ($item){
            if($item['status'] == 2) {
                $item['status_name'] = '待发货';
            } elseif($item['status'] == 3){
                $item['status_name'] = '待收货';
            } elseif($item['status'] == 4){
                $item['status_name'] = '已完成';
            } elseif($item['status'] == 5){
                $item['status_name'] = '已申请退款';
            } elseif($item['status'] == 6){
                $item['status_name'] = '退款完成';
            }
            if($item['payment'] == 1) {
                $item['payment_name'] = '微信支付';
            } elseif($item['payment'] == 2) {
                $item['payment_name'] = '余额支付';
            }
        })->toArray();
    }

    /**
     * 设置订单快递信息
     * @validate('CheckOrderPost')
     */
    public function updateGoods($order_id,$post_name,$post_no)
    {
        $info = GoodsOrder::get($order_id);
        if($info) {
            $GoodsOrder = new GoodsOrder();
            $GoodsOrder->save([
                'post_name' => $post_name,
                'post_no' => $post_no
            ],[
                'id' => $order_id
            ]);
            return writeJson(202, [], '设置成功！');
        } else {
            throw new ParameterException([
                'msg' => [
                    'order' => '请重新选择订单'
                ]
            ]);
        }
    }

    /**
     * 设置订单快递信息
     */
    public function updateGoodsOne($order_id)
    {
        $info = GoodsOrder::get($order_id);
        if($info) {
            if($info['status'] != 2) {
                throw new ParameterException([
                    'msg' => [
                        'order' => '订单状态错误'
                    ]
                ]);
            }
            $GoodsOrder = new GoodsOrder();
            $GoodsOrder->save([
                'status' => 3,
            ],[
                'id' => $order_id
            ]);
            return writeJson(202, [], '发货成功！');
        } else {
            throw new ParameterException([
                'msg' => [
                    'order' => '请重新选择订单'
                ]
            ]);
        }
    }

    /**
     * 获取洗衣订单列表
     * @auth('洗衣订单列表','订单列表')
     */
    public function getStoreList($page,$limit,$user_no,$name,$mobile,$address,$status,$start_date,$end_date,$lin_user_id)
    {
        $where = [];
        $linUser = LinUser::where('id',$lin_user_id)->find();
        if($linUser['admin'] == 1) {
           $where[] = ['lin_user_id','eq',$lin_user_id];
        }
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($mobile) {
            $where[] = ['mobile','like','%'.$mobile.'%'];
        }
        if($address) {
            $where[] = ['address','like','%'.$address.'%'];
        }
        if($status) {
            $where[] = ['store_order.status', 'eq', $status];
        }
        if($start_date) {
            $where[] = ['store_order.create_time', 'gt', strtotime($start_date)];
        }
        if($end_date) {
            $where[] = ['store_order.create_time', 'lt', strtotime($end_date)];
        }
        $query = StoreOrder::hasWhere('user',function ($query1) use($user_no){
            if($user_no) {
                $query1->where('user_no','like','%'.$user_no.'%');
            }
        })->with('content,user')->where($where)->order('store_order.create_time desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '待取送员抢单';
            } elseif($item['status'] == 2){
                $item['status_name'] = '待上门取衣';
            } elseif($item['status'] == 3){
                $item['status_name'] = '待支付';
            } elseif($item['status'] == 4){
                $item['status_name'] = '待用户预约配送';
            } elseif($item['status'] == 5){
                $item['status_name'] = '正在配送';
            } elseif($item['status'] == 6){
                $item['status_name'] = '已完成';
            }
            if($item['payment'] == 1) {
                $item['payment_name'] = '微信支付';
            } elseif($item['payment'] == 2) {
                $item['payment_name'] = '余额支付';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取洗衣订单列表
     */
    public function getStoreExcelList($user_no,$name,$mobile,$address,$status,$start_date,$end_date,$lin_user_id)
    {
        $where = [];
        if ($lin_user_id > 1) {
            $where[] = ['lin_user_id', 'eq', $lin_user_id];
        }
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($mobile) {
            $where[] = ['mobile', 'like', '%' . $mobile . '%'];
        }
        if ($address) {
            $where[] = ['address', 'like', '%' . $address . '%'];
        }
        if ($status) {
            $where[] = ['store_order.status', 'eq', $status];
        }
        if ($start_date) {
            $where[] = ['store_order.create_time', 'gt', strtotime($start_date)];
        }
        if ($end_date) {
            $where[] = ['store_order.create_time', 'lt', strtotime($end_date)];
        }
        return StoreOrder::hasWhere('user',function ($query1) use($user_no){
            if($user_no) {
                $query1->where('user_no','like','%'.$user_no.'%');
            }
        })->with('content,user')->where($where)->order('store_order.create_time desc')->select()->each(function ($item) {
            if ($item['status'] == 1) {
                $item['status_name'] = '待取送员抢单';
            } elseif ($item['status'] == 2) {
                $item['status_name'] = '待上门取衣';
            } elseif ($item['status'] == 3) {
                $item['status_name'] = '待支付';
            } elseif ($item['status'] == 4) {
                $item['status_name'] = '待用户预约配送';
            } elseif ($item['status'] == 5) {
                $item['status_name'] = '正在配送';
            } elseif ($item['status'] == 6) {
                $item['status_name'] = '已完成';
            }
            if ($item['payment'] == 1) {
                $item['payment_name'] = '微信支付';
            } elseif ($item['payment'] == 2) {
                $item['payment_name'] = '余额支付';
            } elseif ($item['payment'] == 2) {
                $item['payment_name'] = '线下现金支付';
            }
        })->toArray();
    }


    /**
     * 删除订单
     * @validate('IdMustBePositiveInt')
     */
    public function deleteStoreOrder($id)
    {
        $info = StoreOrder::get($id);
        if(!$info) {
            throw new ParameterException([
                'msg' => [
                    'store_order' => '订单信息错误'
                ]
            ]);
        }
        if($info['status'] != 1) {
            throw new ParameterException([
                'msg' => [
                    'store_order' => '订单状态错误'
                ]
            ]);
        }
        StoreOrder::destroy($id);
        return writeJson(203, [], '删除成功！');
    }


    /**
     * 商城订单退款操作
     * @validate('IdMustBePositiveInt')
     */
    public function refundGoods($id)
    {
        $info = GoodsOrder::get($id);
        if(!$info) {
            throw new ParameterException([
                'msg' => [
                    'goods_order' => '订单信息错误'
                ]
            ]);
        }
        if($info['status'] != 5) {
            throw new ParameterException([
                'msg' => [
                    'store_order' => '订单状态错误'
                ]
            ]);
        }
        if($info['payment'] == 1) {
            $data =  json_decode(https_post("https://chaojidai.simexcn.com/api/v1/goods_order_refund/admin",[
                'order_id' => $id,
                'string' => 'N#l0owmG82aDGxH'
            ]),true);
            if($data['error_code'] != 202) {
                throw new ParameterException([
                    'msg' => [
                        'goods_order' => $data['msg']
                    ]
                ]);
            } else {
                return $data;
            }
        } elseif($info['payment'] == 2) {
            $user = User::get($info['user_id']);
            Db::startTrans();
            try {
                GoodsOrder::where('id', $info['id'])->update([
                    'admin_refund_time' => time(),
                    'status' => 6,
                ]);
                UserMoney::create([
                    'user_id' => $info['user_id'],
                    'money' => $info['price'],
                    'type' => 7,
                    'user_money' => $user['money'],
                ]);
                User::where('id',$user['id'])->update([
                    'money' => round($user['money']+ $info['price'],2)
                ]);
                Db::commit();
                return writeJson(202, [], '退款成功!');
            }
            catch (Exception $ex) {
                Db::rollback();
                throw $ex;
            }
        } else {
            throw new ParameterException([
                'msg' => [
                    'goods_order' => '订单信息错误'
                ]
            ]);
        }
    }

    /**
     * 商城订单还原操作
     * @validate('IdMustBePositiveInt')
     */
    public function refundGoodsOne($id)
    {
        $info = GoodsOrder::get($id);
        if(!$info) {
            throw new ParameterException([
                'msg' => [
                    'goods_order' => '订单信息错误'
                ]
            ]);
        }
        if($info['status'] != 5) {
            throw new ParameterException([
                'msg' => [
                    'store_order' => '订单状态错误'
                ]
            ]);
        }
        GoodsOrder::where('id', $info['id'])->update([
            'admin_refund_time' => time(),
            'status' => 2,
        ]);
        return writeJson(202, [], '还原成功!');
    }
}